package cn.com.huzo.webapp.controller.manufacture;

import cn.com.huzo.model.entities.manufacture.CmtSettlementSlipHeaderEntity;
import cn.com.huzo.service.manufacture.CmtSettlementSlipHeaderManager;
import huzofw.common.web.controller.BaseFormController;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 加工费计算单画面控制器
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/manufacture/cmt_settlement_slip")
public class CmtSettlementSlipController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "CmtSettlementSlipHeaderManager")
    private CmtSettlementSlipHeaderManager _manager_header;
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到加工费计算单一览画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("manufacture.cmt_settlement_slip.list.def");
        return view;
    }

    /**
     * 跳转加工费计算单编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作类别 1：增加 2：编辑
     * @param slipId 加工费计算单ID
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/{action}/{slipId}/edit.html")
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "slipId") Long slipId) throws Exception {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量
        view.addObject("action", action);
        view.addObject("headerId", slipId);
        view.setViewName("manufacture.cmt_settlement_slip.edit.def");
        return view;
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="主表-数据处理部分">
    /**
     * 得到新的单据号
     * <p>
     * @return @throws Exception
     */
    @RequestMapping(value = "/header/fetchNewBillNo.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchNewBillNo() throws Exception {
        Map<String, Object> _result = new HashMap();
        _result.put("BillNo", _manager_header.fetchNewBillNo());
        return _result;
    }

    /**
     * 根据ID取得加工承揽合同数据。
     *
     * @param criterion 加工承揽合同ID、当前用户、流程标识
     * @return 加工承揽合同数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByHeaderKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        //return _manager_header.fetchByHeaderKey(
        //        MapUtils.getLong(criterion, "HeaderId"),
        //        MapUtils.getLong(criterion, "CurrentUserId"),
        //        MapUtils.getLong(criterion, "WorkFlowId"));
        return null;
    }

    /**
     * 根据条件取得加工承揽合同信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 加工承揽合同信息分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/header/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchForPage(criterion);
    }

    /**
     * 保存加工承揽合同信息数据。
     * <p>
     * @param instance 要保存的加工承揽合同信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/header/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody final CmtSettlementSlipHeaderEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getHeaderId() > 0) { //编辑
            rowsAffected = (long) _manager_header.update(instance);
        } else {                         // 新增
            rowsAffected = _manager_header.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 删除加工承揽合同信息。
     *
     * @param criterion 要删除的数据
     * @return 删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.delete(criterion);
    }
    // </editor-fold>
}
